package org.hadoop.zk;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.util.concurrent.CountDownLatch;
public class ReadData {
    public static void main(String[] args) throws Exception {
        //定义一个线程计数器，用于线程通讯
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zooKeeper = new ZooKeeper("server101:2181", 30000, (e) -> {
            if (e.getState() == Watcher.Event.KeeperState.SyncConnected) {
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();//等待
        byte[] bs = zooKeeper.getData("/test", null, new Stat());
        String str = new String(bs);//将读取到的数据放到String对象中
        System.out.println("读取到的数据是：" + str);
        zooKeeper.close();
    }
}
